const fs = require("fs");
const path = require("path");
const chalk = require('chalk')
const globalConfig = require("../config/conf.json")

// 判断文件或路径是否存在
const fileExists = (path) => {
    return fs.existsSync(path)
}

// 读取一层目录
const readDir = (dirPath) => {
    return fs.readdirSync(dirPath);
}
// 获取所有层目录的所有文件
const readFiles = (dirPath) => {
    const files = readDir(dirPath);
    const filesArray = [];
    files.forEach((item) => {
        const itemPath = path.join(dirPath, item);
        const stats = fs.statSync(itemPath);
        if (stats.isDirectory()) {
            filesArray.push(...readFiles(itemPath));
        } else {
            filesArray.push(itemPath);
        }
    })
    return filesArray;
}
// 获取软件配置
const loadConfig = (configPath) => {
    if (!configPath) {
        return undefined;
    };
    const rConfigPath = path.resolve(configPath);
    if (!fileExists(rConfigPath)) {
        console.error(chalk.red("文件不存在"))
        process.exit(1)
        // return undefined;
    }
    const stat = fs.statSync(rConfigPath)
    if (stat.isFile()) {
        delete require.cache[rConfigPath]
        return require(rConfigPath)
    }
    console.error(chalk.red("路径不是一个文件"))
    process.exit(1)
    // return undefined;
}

// 验证数组是否全部必填
const verifyArgArr = function (args, validArgs) {
    let result = true
    validArgs.forEach((arg, index) => {
        if (args[index] === undefined) {
            result = false;
            console.error(chalk.red(`${arg} 不可为空`))
            process.exit(1)
        }
    })
    return result;
}

// 验证对象是否全部必填
const verifyArgObj = function (argsObj, validArgs) {
    let result = true;
    validArgs.forEach(function (arg) {
        if (argsObj[arg] === undefined) {
            result = false
            console.error(chalk.red(`${arg} 不可为空`))
            process.exit(1)
        }
    })
    return result
}

// 合并路径
const joinAliPath = (path1, path2) => {
    return path.join(path1, path2).split(path.sep).join('/')
}

// 获取文件路径的阿里前缀
const pathToAliPrefix = (pathStr) => {
    if (!pathStr || pathStr === '/' || pathStr === './') return "";
    let returnPathStr = pathStr
    if (returnPathStr.charAt(pathStr.length - 1) !== '/') returnPathStr += '/';
    if (returnPathStr.startsWith('/')) {
        return returnPathStr.slice(1)
    }
    if (returnPathStr.startsWith('./')) {
        return returnPathStr.slice(2)
    }
    return returnPathStr;
}

// 由阿里前缀获取文件名
const aliPrefixesName = (prefixes, prefix) => {
    return prefixes.map(item => {
        if (prefix) {

            return item.slice(0, item.length - 1).split(prefix)[1]
        }
        return item.slice(0, item.length - 1)
    })
}

// 阿里文件获取名字
const aliObjectsName = (objects, prefix) => {
    return objects.map(item => {
        if (prefix) {
            return item.name.split(prefix)[1]
        }
        return item.name
    })
}

// 由阿里路径获取文件名
const getFileName = (pathStr) => {
    if (!pathStr.match(/\//gm)) return pathStr;
    const pathItems = pathStr.split('/');
    return pathItems[pathItems.length - 1];
}

module.exports = {
    fileExists,
    readDir,
    readFiles,
    loadConfig,
    verifyArgArr,
    verifyArgObj,
    joinAliPath,
    pathToAliPrefix,
    aliPrefixesName,
    aliObjectsName,
    getFileName,
}